package linkedlist;

//                                        模拟实现双向链表
public class MyLinkedList {
    //节点
    static public class Node {
        public String val;
        // prve 指向前一个节点
        public Node prve = null;
        public Node next = null;
        public Node(String val){
            this.val = val;
        }
    }
    // head 指向头节点
    public Node head = null;
    public Node tail = null;
    //头插
    public void addFirst(String val){
        Node newNode = new Node(val);
        //特殊情况
        if (head == null){
            head = newNode;
            tail = newNode;
            return;
        }
        head.prve = newNode;
        newNode.next = head;
        head = newNode;
    }
    //尾插
    public void addLast(String val){
        Node newNode = new Node(val);
        //特殊情况
        if (head == null){
            head = newNode;
            tail = newNode;
            return;
        }
        tail.next = newNode;
        newNode.prve = tail;
        tail = newNode;
    }
    //指定位置插入
    public void add(int index, String val){
        //判断合法性
        int size = size();
        if (index < 0 || index > size){
            throw new IndexOutOfBoundsException("index: " + index + ",size: " + size);
        }
        //特殊情况
        if (index == 0){
            addFirst(val);
            return;
        }
        if (index == size){
            addLast(val);
            return;
        }
        //一般情况
        //找到插入位置的前一个节点
        Node prve = head;
        for (int i = 0; i < index -1; i++){
            prve = prve.next;
        }
        Node next = prve.next;
        Node newNode = new Node(val);
        prve.next = newNode;
        newNode.prve = prve;
        newNode.next = next;
        next.prve = newNode;
    }
    //判断元素是否存在
    public boolean contain(String val){
        for (Node cur = head; cur != null; cur = cur.next){
            if (cur.val.equals(val)){
                return true;
            }
        }
        return false;
    }
    public int indexOf(String val){
        int index = 0;
        for (Node cur = head; cur != null; cur = cur.next){
            if (cur.val.equals(val)){
                return index;
            }
            index++;
        }
        return -1;
    }
    //删除操作
    //头删
    public void removeFirst(){
        //特殊情况
        if (head == null){
            return;
        }
        if (head.next == null)
        {
            head = head.next;
            return;
        }
        //一般情况
        head = head.next;
        head.prve = null;
    }
    //尾删
    public void removeLast(){
        //特殊情况
        if (head == null){
            return;
        }
        if (tail.prve == null){
            head = null;
            tail = null;
            return;
        }
        //一般情况
        tail = tail.prve;
        tail.next = null;
    }
    //指定位置删除
    public void remove(int index){
        //判断合法性
        int size = size();
        if (index < 0 || index >= size){
            throw new IndexOutOfBoundsException("index: " + index + ",size: " + size);
        }
        //特殊情况
        if (head == null){
            return;
        }
        if (index == 0){
            removeFirst();
            return;
        }
        if (index == size - 1){
            removeLast();
            return;
        }
        //一般情况
        //找到要删除的前一个节点
        Node prve = head;
        for (int i = 0; i < index - 1; i++){
            prve = prve.next;
        }
        Node toRemove = prve.next;
        Node next = toRemove.next;
        prve.next = next;
        next.prve = toRemove;
    }
    //指定值来删除
    public void remove(String val){
        //特殊情况
        if (head == null){
            return;
        }
        if (head.val.equals(val)){
            removeFirst();
        }
        if (tail.val.equals(val)){
            removeLast();
        }
        //一般情况
        //先找到被删除元素
        Node toRemove = head;
        for (toRemove = head; toRemove != null; toRemove = toRemove.next){
            if (toRemove.val.equals(val)){
                break;
            }
        }
        //两种情况 1.找到了 2.没找到
        if (toRemove == null){
            return;
        }
        Node prve = toRemove.prve;
        Node next = toRemove.next;
        prve.next = next;
        next.prve = prve;
    }
    //清空链表
    public  void clean(){
        head = null;
        tail = null;
    }

    public int size(){
        int size = 0;
        for (Node cur = head; cur != null; cur = cur.next){
            size++;
        }
        return size;
    }
    public String toString(){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for (Node cur = head; cur != null; cur = cur.next){
            stringBuilder.append(cur.val);
            if (cur.next != null){
                stringBuilder.append(", ");
            }
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }
}
